package rkw.database.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;

import org.apache.commons.lang3.builder.ToStringBuilder;

import core.communication.configurationNode.CNode;
import core.communication.message.Direction;
import core.database.AbstractEntity;
import core.database.NodeType;

@Entity
@Table(name = "node")
public class Node extends AbstractEntity {

	@Column(name = "name", nullable = false)
	private String name;

	@Column(name = "ip", nullable = false, length = 40)
	private String ip;

	@Column(name = "port", nullable = false, length = 5)
	private String port;

	@Column(name = "address", nullable = false)
	private String address;

	@Column(name = "level", nullable = false)
	private int level;

	@Column(name = "nodeType", nullable = false)
	@Enumerated(EnumType.STRING)
	private NodeType nodeType;

	@Column(name = "prefferedParentURI")
	private String prefferedParentURI = null;

	public Node() {
	}

	public Node(String name, String ip, String port, String address, NodeType nodeType, Node parent) {
		super();
		this.name = name;
		this.ip = ip;
		this.port = port;
		this.address = address;
		this.nodeType = nodeType;
	}

	public Node(CNode node, Direction direction) {
		super();
		this.name = node.getName();
		this.address = node.getAddress();
		this.ip = node.getIp();
		this.port = node.getPort();
		this.nodeType = node.getNodeType();
		this.level = direction == Direction.UP ? 1 : -1;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public NodeType getNodeType() {
		return nodeType;
	}

	public void setNodeType(NodeType nodeType) {
		this.nodeType = nodeType;
	}

	public String getPrefferedParentURI() {
		return prefferedParentURI;
	}

	public void setPrefferedParentURI(String prefferedParentURI) {
		this.prefferedParentURI = prefferedParentURI;
	}

	public String getLocatorURI() {
		return Node.getLocatorURI(getIp(), getPort());
	}

	public static String getLocatorURI(String ip, String port) {
		return ip + ":" + port;
	}

	public static String getIp(String locatorUri) {
		return locatorUri.split(":")[0];
	}

	public static String getPort(String locatorUri) {
		return locatorUri.split(":")[1];
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}